package service.impl;

import dao.sysRole.SysRoleMapper;
import dao.sysUser.SysUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pojo.SysRole;
import service.SysRoleService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 崔书健
 * @date 2024/7/22
 */
@Transactional
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysRole> getList(String roleName) {
        return sysRoleMapper.getList(roleName);
    }

    @Override
    public boolean add(SysRole sysRole) {
        boolean result = false;
        try {
            if (sysRoleMapper.add(sysRole) == 1){
                result = true;
                // 测试事务回滚时，打开注释
//                throw new RuntimeException();
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public int upd(SysRole sysRole) {
        return sysRoleMapper.upd(sysRole);
    }

    @Override
    public int del(Integer id) {
        try {
            sysUserMapper.deleteByRoleIdInt(id);
        } catch (Exception e) {
            throw new RuntimeException("删除user表中roleId的数据时出现异常");
        }
        return sysRoleMapper.del(id);
    }

    @Override
    public List<SysRole> list() {
        return sysRoleMapper.list();
    }
}
